package com.example.onedemotest.slice;

import com.example.onedemotest.ResourceTable;
import com.example.onedemotest.SecondAbility;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.OnClickListener;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.*;
import ohos.eventhandler.Logger;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class MainAbilitySlice extends AbilitySlice {

    private final int OPEN_TWO_SLICE_CODE=0x11;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
    }

    Text helloWorld;
    @Override
    public void onActive() {
        super.onActive();
        //切换到新的slice
        helloWorld= (Text) findComponentById(ResourceTable.Id_text_helloworld);
       TextField inputTxt= (TextField) findComponentById(ResourceTable.Id_inputTxt);
       RadioContainer RadioContainer= (RadioContainer) findComponentById(ResourceTable.Id_radioContainer);
        RadioContainer.setMarkChangedListener(new RadioContainer.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(ohos.agp.components.RadioContainer radioContainer, int i) {

            }
        });
        inputTxt.addTextObserver(new Text.TextObserver() {
            @Override
            public void onTextUpdated(String s, int i, int i1, int i2) {

            }
        });
        helloWorld.setClickedListener(component -> {
            helloWorld.setText("被点及");
        });

        //切换到新的page
        Button button = (Button) findComponentById(ResourceTable.Id_startNewPageBtn);
        button.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                            Operation operation=new Intent.OperationBuilder()
                                    .withBundleName(getBundleName())
                                    .withAbilityName(SecondAbility.class.getName())
                    .build();
            Intent intent1 = new Intent();
            intent1.setOperation(operation);
            startAbility(intent1);
            }
        });

        TickTimer tickTimer=new TickTimer(this);
        tickTimer.setCountDown(true);
        tickTimer.setBaseTime(System.currentTimeMillis()+30*1000);
        tickTimer.setFormat("ss秒");
        tickTimer.start();

        Image image=new Image(this);

    }

    @Override
    protected void onResult(int requestCode, Intent resultIntent) {
        super.onResult(requestCode, resultIntent);
        if (requestCode==OPEN_TWO_SLICE_CODE){
            if (resultIntent!=null){
                helloWorld.setText(  resultIntent.getIntParam("data",0));
            }
        }
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onInactive() {
        super.onInactive();
    }

    @Override
    protected void onBackground() {
        super.onBackground();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    public Object getLastStoredDataWhenConfigChanged() {
        return super.getLastStoredDataWhenConfigChanged();
    }

}
